package edu.hit.pact.pgse.crawler.download;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.logging.Log;

import edu.hit.pact.pgse.bean.RawWebPage;
import edu.hit.pact.pgse.crawler.util.CustomHttpClient;
import edu.hit.pact.pgse.util.Globals;

public  class FetchorFactory {
	
	private Log logger;
	
	
	public FetchorFactory(Log logger) {
		this.logger = logger;
	}
	
	public FetchorFactory() {
		
	}


	public static  WebPageFetcher buildWebPageFetcher(){
		/*
		 * Construct HttpClient and WegPageFetcher
		 */
		//logger.info("construct httpclient.");
		int maxPerHostConnection = Globals.CRAWLER_DEFAULT_MAX_PERHOST_CONNECTIONS;
		int maxTotalConnection = Globals.CRAWLER_DEFAULT_MAX_TOTAL_CONNECTIONS;
		int connectTimeout = Globals.CRAWLER_CONNECT_TIMEOUT;
		CustomHttpClient httpClient = new CustomHttpClient(maxPerHostConnection, maxTotalConnection, connectTimeout);
		
		if (Globals.CRAWLER_USE_PROXY) {
			String proxyIp = Globals.CRAWLER_PROXY_IP;
			int proxyPort = Globals.CRAWLER_PROXY_PORT;
			
			//logger.info("set proxy : ip :" + proxyIp + " port :" + proxyPort);			
			httpClient.setProxy(proxyIp, proxyPort);
		}//end if
		
		//logger.info("construct web page fetcher.");
		String defalutCharSet = Globals.CRAWLER_WEB_DEFAULT_CHARSET;
		int readTimeout = Globals.CRAWLER_READ_TIMEOUT;
		int maxContentLength = Globals.CRAWLER_MAX_CONTENT_LENGTH;
		
		WebPageFetcher webPageFetcher = new WebPageFetcher(httpClient, defalutCharSet, readTimeout, maxContentLength);
		return webPageFetcher;
	}
	
	public static void main(String[] args){
		URL url = null;
		try {
			 url = new URL("http://hc.apache.org/httpclient-3.x/apidocs/");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		FetchorFactory factory  = new FetchorFactory();
		WebPageFetcher fetchor = factory.buildWebPageFetcher();
		RawWebPage articlePage = fetchor.fetchURL(url, 3, null, null, null);
		if (null != articlePage) {
			String lastModified = articlePage.getLastModified();
			System.out.println("last_modified : " + lastModified);
			System.out.println("update ? : "
					+ fetchor.checkUpdate(url, lastModified, 3));
		}
	}

}
